package erp

import (
	"context"
	"encoding/json"
	"ez/apps/erp/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Product struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64            `json:"id" bson:"id,omitempty"`
	Name        string           `json:"name" bson:"name"`         //产品名称
	FastName    string           `json:"fastName" bson:"fastName"` //助记名称
	Param       string           `json:"param" bson:"param"`       //产品参数
	Code        string           `json:"code" bson:"code"`         //编码
	Cat         *ProductCategory `json:"cat" bson:"cat"`           //分类
	CatId       int64            `json:"catId" bson:"catId"`       //分类
	Price       float64          `json:"price" bson:"price"`       //价格
	Unit        *Unit            `json:"unit" bson:"unit"`         //主单位
	UnitId      int64            `json:"unitId" bson:"unitId"`     //主单位
	IsOn        bool             `json:"isOn" bson:"isOn"`         //启用
	CreateAt    time.Time        `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time        `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time       `json:"deleteAt" bson:"deleteAt"`
}

func (this *Product) DocName() string { return "Product" }
func (this *Product) GetId() int64    { return this.Id }
func (this *Product) SetId(id int64)  { this.Id = id }
func (this *Product) Create() error {
	return this.GetFactory().Create(this)
}
func (this *Product) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *Product) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Product) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *Product) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Product) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *Product) ToString() string {
	return string(this.ToBytes())
}
func (this *Product) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Product) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Product) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *Product) LoadCat() {
	if this.CatId == 0 {
		return
	}
	this.Cat, _ = NewProductCategoryCrud().FindId(this.CatId)
}
func (this *Product) LoadUnit() {
	if this.UnitId == 0 {
		return
	}
	this.Unit, _ = NewUnitCrud().FindId(this.UnitId)
}
func (this *Product) ClearRelationsBeforeSave() mgo.Doc {
	this.Cat = nil
	this.Unit = nil
	return this
}
func neverUsed_Product() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type ProductAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Product
	Session *ez.Session
}

func NewProductAccessControl(model *Product, action string, session *ez.Session) *ProductAccessControl {
	ctrl := &ProductAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.ProductAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
